package com.example.dubbo.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.dubbo.pojo.Mail;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.stereotype.Component;

/**
 * @author: trtan
 * @date: 2021-06-15 16:54
 **/
@Component
@Service
public class SimpleSendEmailImpl implements SimpleSendEmail {
    /**
     * 邮件服务器地址
     */
    private static final String HOSTNAME = "smtp.qq.com";
    /**
     * 邮件字符编码
     */
    private static final String ENCODING = "UTF-8";
    /**
     * qq客户端授权码
     */
    private static final String AUTHORIZATION_CODE = "xxxx";
    /**
     * qq邮箱地址
     */
    private static final String SENDER_ADDRESS = "xxxx";
    /**
     * 发件人昵称
     */
    private static final String SENDER_NAME = "发件测试";

    @Override
    public void send(Mail mail) {
        SimpleEmail email = new SimpleEmail();
        try {
            email.setHostName(HOSTNAME);
            email.setCharset(ENCODING);
            email.setAuthentication(SENDER_ADDRESS, AUTHORIZATION_CODE);
            email.setFrom(SENDER_ADDRESS, SENDER_NAME);
            email.setSubject(mail.getSubject());
            email.setMsg(mail.getMsg());
            email.addTo(mail.getEmail());
            email.send();
            System.out.println("邮件发送成功");
        } catch (EmailException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败");
        }
    }
}
